@using DNA.Mvc.Controllers
@{ 
    var webContext = WebSiteContext.Current;
    var page = webContext.Page;
    //WebContext.Current.Page;
    bool isDesignMode = false;
    string key = "design";
    if (!string.IsNullOrEmpty(Request.QueryString[key]))
    {
        bool.TryParse(Request.QueryString[key], out isDesignMode);
    }
}
<table style="height: 35px; border: none; border-collapse: collapse;">
    <tr>
        <td style="vertical-align: top;">
            @{
                Ajax.Dna().Menu("sitetoolsmenu")
             .Options(options =>
             {
                 options.TopMenuCssClass = "sitetools-menu";
                 options.TopItemCssClass = "sitetools-menuitem";
                 options.TopItemHoverCssClass = "sitetools-menuitem-hover";
                 options.SubMenuItemCssClass = "sitetools-sub-menuitem";
                 options.SubMenuItemHoverCssClass = "sitetools-sub-menuitem-hover";
                 options.SubMenuCssClass = "sitetools-sub-menu";
             })
             .HtmlAttributes(new { style = "display:inline;overflow:visible;" })
             .Items(siteTools =>
             {
                 siteTools.Add(Html.Global("RES_HOME"), webContext.ApplicationPath, Url.Content("~/content/images/icon_home_16.png"));

                 if (Request.IsAuthenticated)
                 {
                     siteTools.Add(new NavigatableNode()
                     {
                         Text = Html.Global("RES_SITETOOLS"),
                         NavigateUrl = webContext.Web.IsRoot ? Url.Content("~/") : (string.IsNullOrEmpty(webContext.Web.DefaultUrl) ? UrlResolver.Resolve(webContext.Web) + "/index.html" : Url.Content(webContext.Web.DefaultUrl)),
                         ImageUrl = Url.Content("~/Content/Images/process.png")
                     }).Items(root =>
                     {
                         var isDesignAuthorized = webContext.IsAuthorized<WidgetController>("Explorer");
                         var isOwner = HttpContext.Current.User.IsWebOwner();

                         if (isDesignAuthorized || isOwner)
                         {
                             if (page != null)
                             {
                                 if (isDesignMode)
                                 {
                                     string returnUrl = (!string.IsNullOrEmpty(Request.QueryString["returnUrl"])) ? Request.QueryString["returnUrl"] : Url.Content("~/");
                                     var scripts = new System.Text.StringBuilder("$('#WidgetExplorerDlg').dialog('open');");

                                     var addConentNode = new NavigatableNode()
                                         {
                                             Text = Html.Global("RES_ADD_CONTENT"),
                                             NavigateUrl = "javascript:" + scripts.ToString() + "void(0);",
                                             ImageUrl = Url.Content("~/Content/Images/add_content.gif")
                                         };

                                     if (webContext.Web.IsRoot)
                                     {
                                         if (isDesignAuthorized)
                                         {
                                             root.Add(addConentNode);
                                         }
                                     }
                                     else
                                     {
                                         root.Add(addConentNode);
                                     }

                                     root.Add(new NavigatableNode()
                                     {
                                         Text = Html.Global("RES_RETURN"),
                                         NavigateUrl = "javascript:window.location='" + returnUrl + "';void(0);",
                                         ImageUrl = Url.Content("~/Content/Images/back.png")
                                     });
                                 }
                                 else
                                 {
                                     var formattedUrl = (Request.RawUrl.IndexOf("?") < 0) ? Request.RawUrl + "?" : Request.RawUrl + "&";
                                     var designNode = new NavigatableNode()
                                     {
                                         Text = Html.Global("RES_DESIGN_PAGE"),
                                         NavigateUrl = "javascript:window.location='" + formattedUrl + "design=true&returnUrl=" + Request.RawUrl + "';void(0);",
                                         ImageUrl = Url.Content("~/Content/Images/design_content.gif")
                                     };

                                     if (webContext.Web.IsRoot)
                                     {
                                         if (isDesignAuthorized)
                                         {
                                             root.Add(designNode).HtmlAttributes(new { id = "sitetools_menuitem_designpage" });
                                         }
                                     }
                                     else
                                     {
                                         root.Add(designNode).HtmlAttributes(new { id = "sitetools_menuitem_designpage" });
                                     }
                                 }
                             }
                         }

                         if (webContext.IsAuthorized<DynamicUIController>("Create") || HttpContext.Current.User.IsWebOwner())
                             root.Add(Html.Global("RES_CREATEPAGE"), Url.Action("Create", "DynamicUI", new { Area = "" }), Url.Content("~/Content/Images/add_page.png"));

                         if (page != null)
                         {
                             if (webContext.IsAuthorized<DynamicUIController>("Edit") || HttpContext.Current.User.IsWebOwner())
                                 root.Add(Html.Global("RES_EDITPAGE"), Url.Action("Edit", "DynamicUI", new { Area = "", id = page.ID }), Url.Content("~/Content/Images/edit_page.png"));

                         }


                         if (webContext.EnablePersonalWeb && webContext.Web.Type == (int)WebTypes.Personal)
                         {
                             if (webContext.IsAuthorized<SysController>("Index"))
                             {
                                 root.Add(Html.Global("RES_CONSOLE"),
                                     Url.Action("Index", "Sys", new { Area = "", website = "home" }), Url.Content("~/Content/Images/console.gif"));
                             }
                         }
                         else
                         {
                             root.Add(Html.Global("RES_CONSOLE"),
                               Url.Action("Index", "Sys", new { Area = "", website = webContext.Web.Name }), Url.Content("~/Content/Images/console.gif"));
                         }


                         if (webContext.EnablePersonalWeb)
                             root.Add(Html.Global("RES_MYCONSOLE"), Url.RouteUrl("dna_mysite", new { website = Context.Profile.UserName, action = "Index", controller = "Sys", Area = "" })
                                        , Url.Content("~/Content/Images/my_console.gif"));
                         else
                             root.Add(Html.Global("RES_MYCONSOLE"), Url.RouteUrl("default", new { action = "Profile", controller = "Account", Area = "" })
                                       , Url.Content("~/Content/Images/my_console.gif"));

                         root.Add(Html.Global("RES_CHANGEPWD"), Url.Action("ChangePassword", "Account", new { Area = "" })
                                , Url.Content("~/Content/Images/support.png"));
                     });
                 }
             })
             .Render();

                if (Request.IsAuthenticated)
                {
                    if (!isDesignMode)
                    {
                        Ajax.RegisterStartupScript("if (!$('.d-widget-zone').length) $('#sitetools_menuitem_designpage').hide();");
                <div style="clear: left;">
                </div>
                    }
                }
            }
        </td>
        <td style="vertical-align: top;">
            @{ Ajax.Dna().Menu("dna-toolbox-menu")
                    .Options(options =>
                    {
                        options.TopMenuCssClass = "sitetools-menu";
                        options.TopItemCssClass = "sitetools-menuitem";
                        options.TopItemHoverCssClass = "sitetools-menuitem-hover";
                        options.SubMenuItemCssClass = "sitetools-sub-menuitem";
                        options.SubMenuItemHoverCssClass = "sitetools-sub-menuitem-hover";
                        options.SubMenuCssClass = "sitetools-sub-menu";
                    })
            .Items(subItems =>
            {
                var selectedLanguage =
                    //webContext.Languages.Where(lan => lan.Value.ToString().Equals(webContext.Language, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
                    webContext.Languages.Where(lan => lan.Value.ToString().Equals(System.Threading.Thread.CurrentThread.CurrentUICulture.Name, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();

                if (selectedLanguage != null)
                {
                    selectedLanguage.NavigateUrl = "javascript:void(0);";
                    subItems.Add(selectedLanguage)
                        .Items(lans =>
                        {
                            foreach (var lan in webContext.Languages)
                            {
                                if (!lan.Value.Equals(selectedLanguage.Value))
                                {
                                    lans.Add(lan.Title,
                                        Url.Action("ChangeLanguage", "Sys", new { Area = "", lang = lan.Value.ToString(), ReturnUrl = Url.Encode(Request.RawUrl) }).Replace("&", "&amp;") //+ "?lang='+ui.value+'&ReturnUrl='+urlHelper.encode(document.URL);void(0);"
                                        , lan.ImageUrl);
                                }
                            }
                        });
                }
                //           subItems.Add("Get feeds", "javascript:void(0);", Url.Content("~/content/images/rss.png"))
                //.Items(feeds =>
                //{
                //    feeds.Add("DotNetAge News", "#", Url.Content("~/content/images/rss.png"));
                //    feeds.Add("DotNetAge Blogs", "#", Url.Content("~/content/images/rss.png"));
                //    feeds.Add("DotNetAge Forums", "#", Url.Content("~/content/images/rss.png"));
                //});
                if (!Request.IsAuthenticated)
                {
                    subItems.Add(Html.Global("RES_LOGIN"), Url.Action("Login", "Account", new { Area = "", website = "home" }), Url.Content("~/Content/Images/icon_user_disable_16.png"));
                    if (webContext.Web.EnableUserRegistration)
                        subItems.Add(Html.Global("RES_REGISTER"), Url.Action("Register", "Account", new { Area = "", website = "home" }), Url.Content("~/Content/Images/icon_key_16.gif"));
                }
                else
                {

                    var _userName = string.IsNullOrEmpty(Context.Profile["DisplayName"] as string) ? Context.Profile.UserName : Context.Profile["DisplayName"] as string;
                    var _authUser = Membership.GetUser(User.Identity.Name);
                    if (_authUser == null) //Error user in cookie
                        System.Web.Security.FormsAuthentication.SignOut();

                    var _userIcon = _authUser.GetAvatar(16);

                    if (string.IsNullOrEmpty(_userIcon) || (_userIcon.EndsWith("no-avatar.gif", StringComparison.OrdinalIgnoreCase))) _userIcon = "~/Content/Images/user_16.png";
                    if (!webContext.EnablePersonalWeb)
                        subItems.Add(_userName, Url.Action("Info", "Account", new { username = Context.Profile.UserName, Area = "" }), Url.Content(_userIcon));
                    else
                        subItems.Add(_userName, UrlHelper.GenerateUrl("dna_mypages", "Index", "DynamicUI",
                            new RouteValueDictionary(new { website = Context.Profile.UserName, layout = "index", Area = "", sites = "sites", extension = "html" }),
                             System.Web.Routing.RouteTable.Routes,
                             Request.RequestContext,
                             true), _userIcon);
                    subItems.Add(Html.Global("RES_SIGNOUT"), Url.Action("LogOff", "Account", new { Area = "", website = "home" }),
                        Url.Content("~/Content/Images/icon_lock.gif"));

                }
                subItems.Add().Template(@<text>
                <form method="post" action="@Url.Content("~/Search")" onsubmit = "if ($('#terms',this).val()=='') { $('#terms',this).focus(); return false;}">
                @(Ajax.Dna().TextBox("terms")
         .AutoComplete(Url.Action("Suggest", "Search", new { Area = "" }))
         .Watermark(Html.Global("RES_SEACHSITE_TIP"))
         .IconImageUrl(Url.Content("~/content/images/search_16.png"))
         .HtmlAttributes(new { onkeypress = "if ((event.keyCode==13) && ($(this).val())) $(this).parents('form').submit();" })
         .GetHtml())
                </form>
                </text>);
            })
          .Render();
            }
        </td>
    </tr>
</table>
@if (isDesignMode)
{
    Ajax.Dna().Dialog("Widget Explorer")
        .Name("WidgetExplorerDlg")
        .Options(opts =>
        {
            opts.AutoOpen = false;
            opts.bgiFrame = true;
            opts.CssClass = "d-dialog";
            opts.Width = 625;
            opts.Height = 440;
            opts.ShowEffect = jQueryEffects.Scale;
            opts.HideEffect = jQueryEffects.Scale;
            opts.IsResizable = false;
            opts.OnClose = "$(\".d-widget-zone\").removeClass(\"d-widget-zone-selected\");";
        })
        .Load(Url.Action("Explorer", "Widget", new { Area = "" }))
        .Render();
    Html.RenderPartial("WidgetClientTemplate");
}
